import Vue from 'vue'
import Router from 'vue-router'
import { nextTick } from 'q';
import store from '../store'

Vue.use(Router)



const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/login',
      name: 'Login',
      component: resolve => require(['components/login/Login.vue'],resolve),
    },
    {
      path: '/company-register',
      name: 'ComRegister',
      component: resolve => require(['components/register/ComRegister'],resolve),
    },
    {
      path: '/personal-register',
      name: 'PerRegister',
      component: resolve => require(['components/register/PerRegister'],resolve),
      children: [
        {
          path: 'select-role',
          name: 'SelectRole',
          component: resolve => require(['components/register/selectRole/SelectRole'],resolve)
        }
      ]
    },
    {
      path: '/resetPwd',
      name: 'ResetPwd',
      component: resolve => require(['components/resetPwd/ResetPwd'],resolve)
    },
    {
      path: '/home',
      name: 'Home',
      component: resolve => require(['components/home/Home'],resolve),
      children: [
        { 
          path: 'control-start',
          name: 'ControlStart',
          component: resolve => require(['components/start/controlStart/ControlStart'],resolve),
          meta: {
            keepAlive: true
          }
        },
        {
          path: 'start-page',
          name: 'Start',
          component: resolve => require(['components/start/Start'],resolve),
          children: [
            { path: 'detail', name: 'Detail', component: resolve => require(['components/start/detail/Detail'],resolve)},
            { path: 'add-start', name: 'AddStartPage', component: resolve => require(['components/start/addstart/AddStartPage'],resolve)}
          ]
        },
        {
          path: 'material',
          redirect: '/material/background',
          component: resolve => require(['components/material/Material'],resolve),
          children: [
            { path: 'background', name: 'BackGround', component: resolve => require(['components/material/background/BackGround'],resolve) },
            { path: 'font', name: 'Font', component: resolve => require(['components/material/font/Font'],resolve) },
            { path: 'topic', name: 'Topic', component: resolve => require(['components/material/topic/Topic'],resolve) },
            { path: 'figure', name: 'Figure', component: resolve => require(['components/material/figure/Figure'],resolve) }
          ]
        },
        {
          path: 'music',
          name: 'Music',
          component: resolve => require(['components/music/Music'],resolve)
        },
        {
          path: 'weather',
          name: 'Weather',
          component: resolve => require(['components/weather/Weather'],resolve)
        },
        {
          path: 'POIsearch',
          name: 'POIsearch',
          component: resolve => require(['components/POIsearch/POIsearch'],resolve)
        },
        {
          path: 'DMSrecord',
          name: 'DMSrecord',
          component: resolve => require(['components/DMSrecord/DMSrecord'],resolve)
        },
        {
          path: 'administration',
          name: 'Administration',
          component: resolve => require(['components/administration/Administration'],resolve),
          children: [
            {
              path: 'userDetail',
              name: 'UserDetail',
              component: resolve => require(['components/administration/userDetail/UserDetail'],resolve)
            }
          ]
        },
        {
          path: 'my-device',
          name: 'Devices',
          component: resolve => require(['components/mydevices/Devices'],resolve),
          children: [
            { path: 'vehicle-detail',
              name: 'VehcleDetail',
              component: resolve => require(['components/mydevices/components/VehcleDetail'],resolve),
              children: [
                {
                  path: 'dangerous-detail',
                  name: 'DangeDetail',
                  component: resolve => require(['components/mydevices/components/DangeDetail'],resolve)
                }
              ]
            }
          ]
        },
        {
          path: 'life',
          name: 'Life',
          component: resolve => require(['components/life/Life'],resolve),
        },
        {
          path: 'setting',
          name: 'Setting',
          component: resolve => require(['components/setting/Setting'],resolve),
          children: [
            {
              path: 'font',
              name: 'SetFont',
              component: resolve => require(['components/setting/setFont/SetFont'],resolve),
            },
            {
              path: 'topic',
              name: 'SetTopic',
              component: resolve => require(['components/setting/setTopic/SetTopic'],resolve),
            },
            {
              path: 'portrait',
              name: 'SetPortrait',
              component: resolve => require(['components/setting/setPortrait/SetPortrait'],resolve),
            }
          ]
        },
        {
          path: 'personal-account',
          name: 'PersonAccount',
          component: resolve => require(['components/perAccount/PersonAccount'],resolve)
        },
        {
          path: 'business-account',
          name: 'BusinAccount',
          component: resolve => require(['components/busAccount/BusinAccount'],resolve),
          children: [
            {
              path: 'permission',
              name: 'Permission',
              component: resolve => require(['components/busAccount/permission/Permission'],resolve)
            }
          ]
        }
      ]
    },
    {
      path: '/404',
      name: 'Page404',
      component: resolve => require(['components/404/404'],resolve)
    },
    {
      path: '*',
      redirect: '/404'
    }
  ]
})


router.beforeEach ((to,from,next) => {
  const whiteRouters = ['Login','ComRegister','PerRegister','SelectRole','ResetPwd']
  let isLogin = store.state.userInfo.token
  if(isLogin){
    if(whiteRouters.indexOf(to.name) >= 0){
      next()
    }else{
      next()
    }
    
  }else{
    if(whiteRouters.indexOf(to.name) >= 0){
      next()
    }else{
      next({ name : 'Login'})
    }
  }
})

export default router
